package com.woniu.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.woniu.controller.form.OrderVo;
import com.woniu.model.Order;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * @author Kevin Kwok
 * @create 2021-04-15 18:53
 */
public interface OrderMapper extends BaseMapper<Order> {
    /**
     * 提供给生成订单的接口
     * @param recId
     * @param uid
     * @return
     */
    @Select("SELECT " +
            "u.address, " +
            "pr.base_price, " +
            "pr.starttime, " +
            "pr.deadline, " +
            "pr.bond, " +
            "pr.transaction_price, " +
            "pr.pid, " +
            "p.product_name, " +
            "p.introduce, " +
            "p.image, " +
            "pr.`status`, " +
            "pr.id AS recId " +
            "FROM " +
            "e_user AS u," +
            "e_product_record AS pr " +
            "INNER JOIN " +
            "e_product AS p " +
            "ON " +
            "pr.pid = p.id " +
            "WHERE " +
            "u.id = #{uid} AND " +
            "pr.id = #{recId}")
    OrderVo createOrder(Integer uid, Integer recId);
    /**
     * 前端获取待付款的全部订单
     * @param uid
     * @return
     */
    @Select("SELECT " +
            "p.image, " +
            "p.product_name, " +
            "p.introduce, " +
            "pr.starttime, " +
            "pr.deadline, " +
            "pr.base_price, " +
            "pr.bond, " +
            "o.price, " +
            "o.pid, " +
            "u.address, " +
            "o.order_status ," +
            "o.id as oid "+
            "FROM " +
            "e_product AS p " +
            "INNER JOIN " +
            "e_product_record AS pr " +
            "ON " +
            "p.id = pr.pid " +
            "INNER JOIN " +
            "e_order AS o " +
            "ON " +
            "pr.pid = o.pid " +
            "INNER JOIN " +
            "e_user AS u " +
            "ON " +
            "o.uid = u.id " +
            "WHERE " +
            "o.uid = #{uid} AND " +
            "o.order_status = #{orderStatus}")
    List<OrderVo> getOrders(Integer uid, Integer orderStatus);
    /**
     * 根据用户id获取全部订单信息
     * @param uid
     * @return
     */
    @Select("SELECT " +
            "p.image, " +
            "p.product_name, " +
            "p.introduce, " +
            "pr.starttime, " +
            "pr.deadline, " +
            "pr.base_price, " +
            "pr.bond, " +
            "o.price, " +
            "o.pid, " +
            "u.address, " +
            "o.order_status ," +
            "o.id as oid "+
            "FROM " +
            "e_product AS p " +
            "INNER JOIN " +
            "e_product_record AS pr " +
            "ON " +
            "p.id = pr.pid " +

            "INNER JOIN " +
            "e_order AS o " +
            "ON " +
            "pr.pid = o.pid " +
            "INNER JOIN " +
            "e_user AS u " +
            "ON " +
            "o.uid = u.id " +
            "WHERE " +
            "o.uid = #{uid}")
    List<OrderVo> getAllOrders(Integer uid);

    /**
     * 订单创建后过时未付款执行修改订单状态为支付失败
     * @param oid
     */
    @Update("update e_order set order_status=#{status} where id=#{oid}")
    void updateOrderStaus(Integer status,Integer oid);

    /**
     * 创建订单
     * @param order
     * @return
     */
    @Insert("insert into e_order(uid,pid,price,order_status," +
            "deadline,checked_status,auction_begin_time," +
            "result,trade_type,rec_id)values(#{uid},#{pid},#{price}," +
            "#{orderStatus},#{deadline},#{checkedStatus}," +
            "#{auctionBeginTime},#{result},#{tradeType},#{recId})")
    @Options(useGeneratedKeys = true,keyProperty = "id")
    Integer insertOrder(Order order);





}
